#if !defined(_CEVENT_H)
#define _CEVENT_H
#include "../Define/Defines.h"
#if !defined(NULL)
#define NULL 0
#endif

#define EVENT_CLASS_SYSTEM		1//(01)
#define EVENT_CLASS_GAME		2//(10)
#define EVENT_CLASS_ENGINE		3//(11)

#define EVENT_STATE_WAIT		1//(1<<0)
#define EVENT_STATE_DEATH		2//(1<<1)
#define EVENT_STATE_OUT_OF_DATE	4//(1<<2)

#if !defined(EVENT_MAX_RECORD)
#define EVENT_MAX_RECORD 50
#endif

//macro to work with event
#define EVENT_CLASS_BIT_RANGE	2
#define EVENT_ID_BIT_RANGE		30
#define EVENT_MACRO_ISSAMECLASS(eid,cmpclass) ((eid>>EVENT_ID_BIT_RANGE) == cmpclass)
#define EVENT_MACRO_GETCLASSID(id, eclass) (((id&0x1FFFFFFF)|eclass<<EVENT_ID_BIT_RANGE)&0xFFFFFFFF)
#define EVENT_MACRO_GETID(eid) (eid&0x1FFFFFFF)
#define EVENT_MACRO_GETCLASS(eid) ((eid>>EVENT_ID_BIT_RANGE)&0xFFFFFFFF)

#define SYS_EVENT(id) EVENT_MACRO_GETCLASSID(id,EVENT_CLASS_SYSTEM)
#define GAME_EVENT(id) EVENT_MACRO_GETCLASSID(id,EVENT_CLASS_GAME)
#define ENGINE_EVENT(id) EVENT_MACRO_GETCLASSID(id,EVENT_CLASS_ENGINE)



class CEvent{
public:
	unsigned int  _id; // interface of event
	unsigned long _time;//time of event appear
	unsigned int _state; // state of event;
	unsigned int _data_len;//len of data;
	char* _data;// data attach of event;
};
class CTimerEvent{
private:
	CTimerEvent(){};
public:
	int ID;
	static CTimerEvent* read(CEvent* evt){
		if(evt != NULL && evt->_id == ECODE_TIMER_TICK && evt->_data_len >= SIZE_OF_INT32){
			CTimerEvent* rs = new CTimerEvent();
			MEMCOPY(evt->_data,&rs->ID,SIZE_OF_INT32);
			return rs;
		}
		return NULL;
	};
};
class CEventManager{
public:
	int _error_count;
	bool _is_call_system;
	CEventManager();
	~CEventManager();
	CEvent _events[EVENT_MAX_RECORD];
	CEvent* getEvents(int* pointer, unsigned int eid);
	CEvent* getEventOfClass(int* pointer, unsigned int classid);
	void put(unsigned int id, const char* data, int len);
};
#endif